
<!--
 * @Descripttion: 
 * @version: 
 * @Author: wtzhang
 * @Date: 2021-09-08 15:01:07
 * @LastEditors: wtzhang
 * @LastEditTime: 2021-10-20 09:09:51
-->
<template>
	<view class="content">
		<view class="">
			表名
		</view>
		<radio-group @change="radioChange">
			<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in tableList" :key="index">
				<view>
					<radio :value="index" :checked="index == radioIndex" />{{item.name}}
				</view>
			</label>
		</radio-group>
		<button type="primary" @click="dbType">检查数据库打开状态</button>
		<button type="primary" @click="openDb">打开数据库</button>
		<button type="primary" @click="closeDb">关闭数据库</button>
		<input class="input_box" type="text" value="" v-model="valInp" placeholder="请输入聊天内容" />
		<button type="primary" @click="addSql">添加数据</button>
		<button type="primary" @click="setSql">修改数据</button>
		<input class="input_box" type="text" value="" v-model="valTab" placeholder="请输入表名" />
		<button type="primary" @click="addExecuteSql">添加表</button>
		<button type="primary" @click="tapMerge">合并数据</button>
		<button type="primary" @click="delSql">删除数据库数据</button>
		<button type="primary" @click="getTab">查询所有数据表名</button>
		<button type="primary" @click="tapIsTab">查询表是否存在</button>
		<button type="primary" @click="getNum">获取表数据总条数</button>
		<button type="primary" @click="getSql">获取数据库数据</button>
		<button type="primary" @click="getBranch">获取分页数据库数据+排序</button>
		<t-table class="table_box" :chatTableListLength="chatTableListLength" :chatTableList="chatTableList">
		</t-table>
	</view>
</template>

<script>
	import {
		isOpen,
		openDb,
		addTab,
		mergeSql,
		getDataList,
		addTabItem,
		closeSQL,
		deleteInformationType,
		getTable,
		getCount,
		updateSQL,
		isTable,
		selectDataList
	} from '@/js_sdk/wt-sqlite/index.js'
	import tTable from '../../components/p-table/table/table.vue';
	export default {
		components: {
			tTable
		},
		data() {
			return {
				valTab:'',
				radioIndex:0,
				valInp: '',
				chatTableList: [],
				tableList:[],
				chatTableListLength: 0,
				tableName:''
			}
		},
		async onLoad() {
			await this.openDb()
			await this.getTab()
		},
		methods: {
			radioChange(val) {
				this.tableName = this.tableList[val.detail.value].name
				console.log(this.tableList)
			},
			tapMerge() {
				let tabs = [{
					"local_id": "3_wtsu83kox2",
					"id": "3_wtsu83koxx",
					"chat_friend_id": "兔兔",
					"content": 2
				}, {
					"local_id": "3_hq5uyx9vrd",
					"id": "3_hq5uyx9vrd",
					"chat_friend_id": "兔兔",
					"content": 2
				}]
				// 只会插入不重复的
				mergeSql('pop', this.tableName, tabs).then((res) => {
					console.log(res)
				})
			},
			tapIsTab() {
				isTable('pop', this.tableName).then((res) => {
					console.log(res)
				})
			},
			getTab() {
				getTable('pop').then((res) => {
					console.log(res)
					this.tableList = res
				})
			},
			addExecuteSql() {
				console.log(this.valTab)
				if(!this.valTab || this.valTab == '') return
				addTab('pop', this.valTab).then(res => {
					console.log(res)
					this.valTab = ''
					this.getTab()
				})
			},
			delSql() {
				deleteInformationType('pop', this.tableName, {
					"chat_i": 2,
				}).then(res => {
					console.log(res)
				})
			},
			dbType() {
				uni.showToast({
					icon: "none",
					title: isOpen('pop') ? '数据库已打开' : '数据库已关闭'
				})
			},
			closeDb() {
				closeSQL().then(res => {
					console.log(res)

				})
			},
			openDb() {
				if (isOpen('pop')) return
				openDb('pop').then(res => {
					console.log(res)
				})
			},
			setSql() {
				updateSQL('pop', this.tableName, {
					chat_friend_id: 'Texas3333666',
					content: '3333666'
				}, 'local_id', '3_bpu8sufahoe').then(res => {
					console.log('1111')
				})
			},
			addSql() {
				console.log(this.valInp)
				console.log(this.tableName)
				let _this = this
				if (this.valInp == '') {
					uni.showToast({
						icon: "none",
						title: "请输入聊天内容"
					})
					return
				}
				let objMsg = {}
				let local_id = 3 + '_' + this.getLocalId()
				let content = {}
				content.voiceTime = 0
				content.url = ''
				content.tempFilePath = ''
				content.text = this.valInp
				objMsg.id = local_id
				objMsg.chat_friend_id = 3
				objMsg.user_id = 1
				objMsg.local_id = local_id
				objMsg.is_read = 0
				objMsg.status = 0
				objMsg.send_time = parseInt(new Date().getTime() / 1000)
				objMsg.msg_type = 1
				objMsg.content = JSON.stringify(content)
				addTabItem('pop', _this.tableName, {
					id: objMsg.id,
					local_id: objMsg.id,
					content: '2',
					chat_friend_id: _this.valInp,
				}).then((res) => {
					_this.valInp = ''
					console.log(_this.valInp)
				})
			},
			// 获取本地id
			getLocalId() {
				return Math.random().toString(36).slice(2)
			},
			getSql() {
				selectDataList('pop', this.tableName,{}).then(res => {
					console.log(res)
					this.chatTableList = res
				})
			},
			getBranch() {
				getDataList('pop', this.tableName, 1, 20, 'chat_i', 'desc').then(res => {
					console.log(res)
					this.chatTableList = res
				}).catch(err => {
					console.log(err)
				})
			},
			getNum() {
				getCount('pop', this.tableName).then(res => {
					this.chatTableListLength = res[0].num
				})
			}
		}
	}
</script>

<style>
	.content button {
		margin-bottom: 20rpx;
	}

	.input_box {
		width: 690rpx;
		height: 80rpx;
		font-size: 40rpx;
		line-height: 40rpx;
		margin: 10rpx auto;
		background-color: pink;
	}

	.table_box {
		width: 690rpx;
		margin: 10rpx auto;
		text-align: center;
	}
</style>
